package com.dcloud.manage.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcloud.manage.request.CollectTaskSqlRequest;
import com.dcloud.manage.vo.CollectTaskSqlVO;
import org.jeecg.common.api.vo.Result;
import com.dcloud.manage.service.entity.CollectTask;
import com.dcloud.manage.service.ICollectTaskService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 同步任务表
 * @Author: jeecg-boot
 * @Date:   2022-09-07
 * @Version: V1.0
 */
@Api(tags="同步任务表")
@RestController
@RequestMapping("/api/task/v1")
@Slf4j
public class CollectTaskController extends JeecgController<CollectTask, ICollectTaskService> {
	@Autowired
	private ICollectTaskService collectTaskService;
	
	/**
	 * 分页列表查询
	 *
	 * @param collectTask
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "同步任务表-分页列表查询")
	@ApiOperation(value="同步任务表-分页列表查询", notes="同步任务表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CollectTask collectTask,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<CollectTaskSqlVO> page = new Page<>(pageNo, pageSize);
		Page<CollectTaskSqlVO> pageList = collectTaskService.pageInfo(page, collectTask);
		return Result.OK(pageList);
	}

	 /**
	  * 不分页列表查询
	  *
	  * @param collectTask
	  * @return
	  */
	 @AutoLog(value = "任务表-不分页列表查询")
	 @ApiOperation(value="同步任务表-分页列表查询", notes="同步任务表-分页列表查询")
	 @GetMapping(value = "/list/nopage")
	 public Result<?> queryList(CollectTask collectTask) {
		 List<CollectTaskSqlVO> list = collectTaskService.listByParams(collectTask);
		 return Result.OK(list);
	 }
	
	/**
	 *   添加
	 *
	 * @param collectTaskSqlRequest
	 * @return
	 */
	@AutoLog(value = "同步任务表-添加")
	@ApiOperation(value="同步任务表-添加", notes="同步任务表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CollectTaskSqlRequest collectTaskSqlRequest) {
		collectTaskService.saveByTaskSqlReq(collectTaskSqlRequest);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param collectTaskSqlRequest
	 * @return
	 */
	@AutoLog(value = "同步任务表-编辑")
	@ApiOperation(value="同步任务表-编辑", notes="同步任务表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CollectTaskSqlRequest collectTaskSqlRequest) {
		collectTaskService.updateByTaskSqlReq(collectTaskSqlRequest);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "同步任务表-通过id删除")
	@ApiOperation(value="同步任务表-通过id删除", notes="同步任务表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		collectTaskService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "同步任务表-批量删除")
	@ApiOperation(value="同步任务表-批量删除", notes="同步任务表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.collectTaskService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "同步任务表-通过id查询")
	@ApiOperation(value="同步任务表-通过id查询", notes="同步任务表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CollectTask collectTask = collectTaskService.getById(id);
		if(collectTask==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(collectTask);
	}

	 /**
	  * 根据任务的状态来查询任务列表
	  *
	  * @return
	  */
	 @AutoLog(value = "同步任务表-通过id查询")
	 @ApiOperation(value="同步任务表-通过id查询", notes="同步任务表-通过id查询")
	 @PostMapping(value = "/list_by_state")
	 public Result<?> listByState(@RequestParam(name="taskState", defaultValue="running") String taskState) {
		 List<CollectTask> collectTasks = collectTaskService.list(new LambdaQueryWrapper<CollectTask>()
				 .eq(CollectTask::getTaskState,taskState));
		 return Result.OK(collectTasks);
	 }


}
